(function($){

    $.fn.extend({
        allower : function(options, arg) {
            if (typeof(options) == "object") {
                options = $.extend({}, $.allower.defaults, options);
            }
            this.each(function() {
                for (var fld in options) {
                    jQuery(this).attr("plugin_allower_" + fld, options[fld]);
                }
                new $.allower(this, options, arg);
            });
            return;
        }
    });

    var allower_click = function(item, options) {
        var parentItem = null;
        if (options.type == "row") {
            parentItem = jQuery(item).parents("tr").first();
        } else if (options.type == "cell") {
            parentItem = jQuery(item).parents("td").first();
        } else {
            return;
        }
        if (item.checked) {
            parentItem.find("input").each(function(ndx, i) {
                if (jQuery(i).attr("name") == item.name) { return; }
                i.disabled = false;
            });
            parentItem.find(".fileuploader").each(function(ndx, i) {
                fileuploader_enable(i);
            });
        } else {
            parentItem.find("input").each(function(ndx, i) {
                if (jQuery(i).attr("name") == item.name) { return; }
                i.disabled = true;
            });
            parentItem.find(".fileuploader").each(function(ndx, i) {
                fileuploader_disable(i);
            });
        }
    }

    $.allower = function(item, options, arg) {
        var myName = item.name;
        if (typeof(options) == "string") {
            var methodName = options;
            options = {};
            for (var fld in $.allower.defaults) {
                options[fld] = jQuery(item).attr("plugin_allower_" + fld);
            }
            // methods
        } else {
            allower_click(item, options);
            jQuery(item).click(function() {
                allower_click(item, options);
            });
        }

        $.allower.defaults = {
            type : "row"
        }
    }
})(jQuery);
